package com.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.basic.domain.ApiResult;
import com.basic.domain.PageResult;
import com.demo.service.IOrderService;
import com.demo.service.criteria.OrderQueryCriteria;
import com.demo.service.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-12
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("/getOrderByPage")
    public ApiResult<PageResult<OrderDto>> getOrderByPage(@RequestBody OrderQueryCriteria criteria, Pageable pageable) {
        return ApiResult.ok(orderService.queryAll(criteria, pageable));
    }

    @GetMapping("/getOrderById")
    public ApiResult<OrderDto> getOrderById(@RequestParam Long id) {
        return ApiResult.ok(orderService.queryById(id));
    }

    @PostMapping("getOrderOne")
    public ApiResult<OrderDto> getOrderOne(@RequestBody OrderQueryCriteria criteria) {
        return ApiResult.ok(orderService.queryOne(criteria));
    }

    @PostMapping("/saveOrUpdateOrder")
    public ApiResult<OrderDto> saveOrUpdateOrder(@RequestBody OrderDto OrderDto) {
        return ApiResult.ok(orderService.saveOrUpdate(OrderDto));
    }

    @GetMapping("/deleteOrderById")
    public ApiResult<Object> deleteOrderById(@RequestParam Long id) {
        return ApiResult.ok(StrUtil.format("成功删除{}条数据", orderService.deleteId(id)));
    }

    @PostMapping("/deleteOrderMore")
    public ApiResult<Object> deleteOrderMore(@RequestBody OrderQueryCriteria criteria) {
        orderService.deleteByMore(criteria);
        return ApiResult.ok("删除成功");
    }
}
